/**********************************************************************/
/*
Author: Karan Makkar
Created: Nov 2023
Updated: Aug 2025, by Youssef Assarssah
Description: Demographics by Online ’22 Survey Response
Output: TableA27
*/
/**********************************************************************/


/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_pmovars_by_online22response.txt", text replace

global tabvars age ///
            female ///
            urban ///
            java ///
            yrs_school ///
            final_test_score

* Include makestars program
  include "$KP_do/3 Functions/makestars.do"

/*----------------------------------------------------------------*/
* Section: Data Setup
/*----------------------------------------------------------------*/

* Load 2021 data
use "$KP_deid_survey/2021/Clean/071323_survey_21_deid_clean_merged.dta", clear

 *Keep only those sent survey.
keep if sent_survey==1 | !mi(progress)

keep if ever_win_17 ==0 

replace gender = 2 if gender == 0

tempfile t21
sa `t21', replace

* Merge in non-missing

* Load 2022 data
use "$KP_deid_survey/2022/Clean/072125_survey_21_22_deid_clean_control_merged.dta", clear

bysort anon_id4 (batch): keep if _n ==1

* Keeping only those sent survey
assert sent_survey ==1 | !mi(progress)

* Flag respondents
gen respondent = completed == 1 & consented == 1 & verified == 1

merge 1:1 anon_id4 using `t21', assert(2 3) nogen keepusing(age gender urban java yrs_school final_test_score)

replace respondent = 0 if mi(respondent)

replace yrs_school = 13 if yrs_school==13.5

gen female = gender ==2

label var age "Age"
label var female "Female"
label var urban "Urban"
label var java "Java"
label var yrs_school "Years of School"
label var final_test_score "Skills Test Score"

foreach v of varlist $tabvars {
        local `v'_label: var label `v'
    }

    * Sum Table 
    local i=-1
    local j=0

    * initialize elements for list
    local format = "%12.3fc"
    global outcome_names = `"   "'
    tokenize `"$tabvars"'
    local num : word count $tabvars
    forval z = 1/`num' {
        local outcome`z' = "``z''"
    }
            
    foreach var in $tabvars {

        local i=`i'+2
        local j=`j'+2
        local f = `j'+2

        di "`var'"

        local r`i'_c0: variable label `var'
        
        di "`r`i'_c0'"

        su `var'

        if (`r(mean)'!=0) & (`r(N)'!=0) {

        *Mean  - all
        ** Report applied
        summ `var' if respondent==1
        local r`i'_c1 = string(r(mean),"%4.3fc")
        local r`j'_c1 = "(" + string(r(sd),"%4.3fc") + ")"

        ** DO NOT Report applied
        summ `var' if respondent==0
        local r`i'_c2 = string(r(mean),"%4.3fc")
        local r`j'_c2 = "(" + string(r(sd),"%4.3fc") + ")"

        ** regressing to get t-stats

        reg `var' respondent, r

            local beta =  r(table)[1,1]
            local pval = r(table)[4,1]
            local se = r(table)[2,1]
            local tstats = r(table)[3,1]
            
            local r`j'_c3 = "(" + string(`se',"%4.3fc") + ")"
            local r`f'_c3 = string(e(N), "%9.0fc") 

            makestars, pointest(`beta') pval(`pval') bdec(3)
            di "`r(coeff)'"
            local r`i'_c3 = r(coeff)
        }
    }

    * final row with observations
        local f=`j'+2
        local f_1=`j'+3

    * to know the number of variables displayed in total in the balance table
    di "`i' : number of variables "

    * Get observation counts
    summ final_test_score if respondent==1
        local r`f'_c1 = string(r(N), "%9.0fc")
        di "`r`f'_c1'"
        
    summ final_test_score if respondent==0
        local r`f'_c2 = string(r(N), "%9.0fc")    
        di "`r`f'_c2'"    

    // ** write table header

    local tex_file = "$KP_output/tables/descriptive/tex/TableA27.tex"

    
    tempname myfile
    #delimit ;
    file open `myfile' using "`tex_file'", write replace;
    file write `myfile'
    _n "\hline \hline"
    _n " Variable & Respondent & Non-Respondent & Difference \\"
    _n " & (1) & (2) & (3) \\"
    _n " \hline"
    _n "`paneltext'"

    ;
    file close `myfile' ;
    #delimit cr

    ** write table body

    di "`=`num'*2'"

    forval row = 1 / `=`num'*2' {
        tempname myfile
        
        #delimit ;
        file open `myfile' using "`tex_file'", write append;
        file write `myfile'
        _n "`r`row'_c0' & `r`row'_c1' & `r`row'_c2' & `r`row'_c3' \\ " ;
        file close `myfile';
        #delimit cr
    }

    ** write table footer
    tempname myfile
    #delimit ;
    file open `myfile' using "`tex_file'", write append;
    file write `myfile'
    _n "\\ Observations & `r`f'_c1' & `r`f'_c2' & `r`f'_c3' \\"
    _n "Batches & 18-22 & 7-22 & 7-22 \\"
    _n "\hline"
    ;
    file close `myfile';
    #delimit cr